package com.jumi.microservice.common.config.rocketmq.configuration;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.jumi.microservice.common.config.rocketmq.AliYunRocketMqConfig;
import com.jumi.microservice.common.core.aspect.handler.DataUpdateInterceptor;
import com.jumi.microservice.common.exception.GlobalExceptionHandler;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * - - - - - - - - - - - - - - -
 *
 * @author liuz
 * @date 2020/7/25 09:15
 * ToDoSomeThing
 * - - - - - - - - - - - - - - -
 */
@Configuration
public class ProviderConfig {

    private static final Logger log = LoggerFactory.getLogger(ProviderConfig.class);

    @Autowired
    private MqConfig mqConfig;

//    /**
//     * 创建普通消息发送者实例
//     *
//     * @return
//     * @throws
//     */
//    @Bean
//    public DefaultMQProducer defaultProducer() throws MQClientException {
//        log.info(mqConfig.toString());
//        log.info("defaultProducer 正在创建---------------------------------------");
//        DefaultMQProducer producer = new DefaultMQProducer();
//        producer.setNamesrvAddr(mqConfig.getNamesrvAddr());
//        producer.setProducerGroup(mqConfig.getGroupName());
//        producer.setVipChannelEnabled(false);
//        producer.setRetryTimesWhenSendAsyncFailed(10);
////        producer.start();
//        log.info("rocketmq producer server开启成功---------------------------------.");
//        return producer;
//    }

    /**
     * 阿里云mq生产者
     * @return
     */
    @Bean(name="log_producer")
    public Producer aliRocketMqProducer(){
        log.info("aliRocket Producer 正在创建---------------------------------------");
        Properties properties = getMqProperties();
        Producer producer = ONSFactory.createProducer(properties);
        // 在发送消息前，必须调用 start 方法来启动 Producer，只需调用一次即可
        producer.start();
        log.info("aliRocket producer server开启成功---------------------------------.");
        return producer;
    }

    private Properties getMqProperties(){
        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, mqConfig.getAccessKey());
        properties.setProperty(PropertyKeyConst.SecretKey, mqConfig.getSecretKey());
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, mqConfig.getNameSrvAddr());
        //操作日志的groupid
        properties.put(PropertyKeyConst.GROUP_ID, mqConfig.getGroupId());
        return properties;
    }


    /**
     * 拦截器操作
     * @return
     */
    @Bean
    public DataUpdateInterceptor dataUpdateInterceptor() {
        DataUpdateInterceptor dataUpdateInterceptor = new DataUpdateInterceptor();
        return dataUpdateInterceptor;
    }
}
